package com.baibei.widget;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

/**
 * Created by tang on 2018/7/18.
 */

public class InterceptScrollView extends NestedScrollView {
    private GestureDetector mGestureDetector;

    @SuppressWarnings("deprecation")
    public InterceptScrollView(Context context, AttributeSet attrs) {

        super(context,attrs);

        mGestureDetector= new GestureDetector(new YScrollDetector());

        setFadingEdgeLength(0);

    }
    //通过手势判断，来判断是否拦截触摸事件。
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }
}
